﻿using System;
using System.Collections.Generic;

using E.BLL;
using E.Model;
using E.Web.Common;
using E.Web.UI.Page;

using X.Common;

namespace E.Web.Teacher.publics.certificate
{
    public partial class Relation : TeacherBasePage
    {
        public int id;
        public CertificateModel model = new CertificateModel();
        public Dictionary<int, string> courseList;
        public string course_v;

        protected override void ShowPage()
        {
            id = XRequest.GetInt("id", -1);

            CourseBLL bll = new CourseBLL();

            if (ispost)
            {
                if (ValidateForm())
                {
                    int rst = 0;
                    string msg = "";
                    string href = "";

                    string course = XRequest.GetString("course_v");
                    string[] courses = GetArray(course);

                    rst = bll.EditRelation(id, courses);
                    if (rst > 0)
                    {
                        msg = "更新成功";
                        href = "window.location.replace('/publics/certificate/Relation.aspx?id=" + id + "');";
                    }
                    else
                    {
                        msg = "更新失败，请稍后再试";
                        href = "history.back();";
                    }

                    ExecuteJS("alert('" + msg + "'); " + href);
                }
                else
                    ExecuteJS("history.back();");
            }
            else
            {

                if (id != -1)
                {
                    model = bll.GetCertificateProjectInfoById(id);
                    courseList = bll.GetCourseList();
                    course_v = WebCommon.Convert2String(bll.GetCourseIDListByCertificateID(id));
                    if (model.ID == -1)
                    {
                        ExecuteJS("alert('数据读取错误'); window.location = '/publics/certificate/List.aspx';");
                    }
                }
            }
        }

        private bool ValidateForm()
        {
            bool rst = true;

            if (XRequest.GetString("course_v").Equals(""))
            {
                ExecuteJS("alert('请选择至少一个课程');");
                rst = false;
            }

            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
